package com.leetcode.leetcode2;

/**
 *  移除元素
 * @author: luhd
 * @create: 2021-09-12
 */
public class Practice27 {
    public static void main(String[] args) {
        int[] nums = {3,2,2,3};
        int val = 3;
    }
    public static int removeElement(int[] nums, int val) {
        int left = 0;
        int right = 0;
        while(right<nums.length){
            if(nums[right]!=val){
                nums[left++] = nums[right++];
            } else{
                right++;
            }
        }
        return left;
    }
    // 双指针解法优化，头尾遍历
    /*public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }*/
}
